package com.wonder.framework.aws.utils;

import java.util.Arrays;

public class FileUtil {
    /**
     * 把字节B转换成KB、MB、GB
     * @param size 字节
     */
    public static String calSize(Long size){
        if (size < 1024) {
            return size + " B";
        } else {
            size = size / 1024;
        }

        if (size < 1024) {
            return size + " KB";
        } else {
            size = size / 1024;
        }

        if (size < 1024) {
            size = size * 100;
            return size / 100 + "." + size % 100 + " MB";
        } else {
            size = size * 100 / 1024;
            return size / 100 + "." + size % 100 + " GB";
        }
    }

    /**
     * 拆分byte数组
     * @param bytes byte数组
     * @param subSize 每份数组大小
     */
    public static byte[][] splitByteArr(byte[] bytes, int subSize){
        int splitLength = bytes.length % subSize == 0 ? bytes.length / subSize : bytes.length / subSize + 1;
        byte[][] result = new byte[splitLength][];
        int from, to;
        for(int i = 0; i < splitLength; i++){
            from  = i * subSize;
            to = from + subSize;
            if(to > bytes.length){
                to = bytes.length;
            }
            result[i] = Arrays.copyOfRange(bytes,from, to);
        }

        return result;
    }
}
